
package com.ruijie.acl.web.validator;

import java.util.Map;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import org.apache.commons.lang.StringUtils;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.faces.Validator;

import com.ruijie.acl.type.PortTypeEnum;
import com.ruijie.acl.utils.AclUtil;
import com.ruijie.emp.utils.NetUtils;
import com.ruijie.foundation.international.MessageSupport;

/**
 * <p>
 * Title: 模板中ACL规则的栏位值验证

 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * 模板中ACL规则的栏位值验证
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-07-14
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * @author ASAI
 */
@Name("templateAceValueValidator")
@Validator
public class TemplateAceValueValidator  extends MessageSupport implements
javax.faces.validator.Validator{
	
	@In private Map<String, String> messages;

	//是否包含${...}的正则表达式
	private static Pattern VAR_PATTERN=Pattern.compile(".*\\$\\{[a-zA-Z0-9_]+\\}.*");
	private static Pattern NUM_PATTERN=Pattern.compile("[0-9]+");
	
	public void validate(FacesContext context, UIComponent component,
			Object obj) throws ValidatorException {
		String value=obj.toString().trim();
		
		if(component.getAttributes().get("field")==null)return ;
		
		if(component.getAttributes().get("required").toString().equals("true")){
			if(!StringUtils.isNotEmpty(value.replaceAll("　",   " ").trim())){
				throw new ValidatorException(new FacesMessage(messages.get("javax.faces.component.UIInput.REQUIRED")));
			}
		}
		
		
		String field=component.getAttributes().get("field").toString().trim();
		//如果没有包含变量，进行相应的验证
		if(!VAR_PATTERN.matcher(value).matches()){
			//[start]VLan编 号
			if("vlan".equalsIgnoreCase(field)){
				String vlan=value;
				if(vlan.length()!=0){
					if(!NUM_PATTERN.matcher(vlan).matches()){
						//vlan字段必须填写数值！
						throw new ValidatorException(new FacesMessage(
								getI18NMessage("acl_ace_validate_vlan_require_num")));
					}else{
						int vlanValue=Integer.parseInt(vlan);
						//vlan编号必须在0 ~ 4095之间！
						if(vlanValue<1||vlanValue>4095){
							throw new ValidatorException(new FacesMessage(
									getI18NMessage("acl_ace_validate_vlan_scop")));
						}
					}
				}
			}
			//[end]
			//[start]ip地址
			if("ip".equalsIgnoreCase(field)){
				String ip=value;
				if(ip.length()>0){
					if(!AclUtil.ipV4Validator(ip)){
						throw new ValidatorException(new FacesMessage(
								getI18NMessage("emp_common_validation_ipValidateFailed")));
					}
				}
			}
			//[end]
			//[start]通配符
			if("wildcard".equalsIgnoreCase(field)){
				String wildcard=value;
				if(wildcard.length()>0){
					if(!AclUtil.wildcardValidator(wildcard)){
						throw new ValidatorException(new FacesMessage(
								getI18NMessage("acl_ace_validation_wildcardValidateFailed")));
					}
				}
			}
			//[end]
			//[start]MAC地址
			if("mac".equalsIgnoreCase(field)){
				String mac=value;
				if(mac.length()>0){
					if(!NetUtils.isMACValidate(mac)){
						throw new ValidatorException(new FacesMessage(
								getI18NMessage("emp_device_validation_macValidateFailed")));
					}
				}
			}
			//[end]
			//[start]端口
			if("port".equalsIgnoreCase(field)){
				String port=value;
				if(port.length()>0){
					if(!AclUtil.isNotCNAndFullChar(port)){
						//acl_ace_validate_port_no_cn_full_char=端口值不能输入中文或全角字符！
						throw new ValidatorException(new FacesMessage(
								getI18NMessage("acl_ace_validate_port_no_cn_full_char")));
					}else if(NUM_PATTERN.matcher(port).matches()){
						int portValue=Integer.parseInt(port);
						if(portValue<0||portValue>65535){
							//acl_ace_validate_port_range_num=如果端口为数值，则必须在0 ~ 65535之间！
							throw new ValidatorException(new FacesMessage(
									getI18NMessage("acl_ace_validate_port_range_num")));
						}
					}else{
						if(PortTypeEnum.getEnum(port)==null){
							//acl_ace_validate_port_inside_enum=如果端口为字符，则必须为设备允许的类型
							throw new ValidatorException(new FacesMessage(
									getI18NMessage("acl_ace_validate_port_inside_enum")));
						}
					}
				}
			}
			//[end]
		}
	}
}

















